Completed
Push — master ( d1844c...194c7c )
by
unknown
03:08
created

ql.executeWhereClause() ANDꞌ)   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
nop 0
1
var chai = require('chai');
2
var path = require('path');
3
4
var config = require('../src/cli').config
5
config.set({root: __dirname + '/fixtures'})
0 ignored issues
show
Compatibility introduced by
Consider using the path module for constructing paths since they are otherwise not cross-OS compatible.
Loading history...
6
7
var cmsData = require('../src/cli').cmsData
8
var Util = require('../src/cli').Util
9
var Manager = require('../src/cli').Manager;
10
var fse = require('fs-extra');
11
12
describe('Request', function() {
13
  before( function(done) {
14
    Manager.instance.init()
15
      .then(function () {
16
        Manager.instance._whereKeys = ['title', 'priority', 'abe_meta', 'articles']
17
        Manager.instance.updateList()
18
19
        this.fixture = {
20
          tag: fse.readFileSync(__dirname + '/fixtures/templates/article.html', 'utf8'),
0 ignored issues
show
Compatibility introduced by
Consider using the path module for constructing paths since they are otherwise not cross-OS compatible.
Loading history...
21
          jsonArticle: fse.readJsonSync(__dirname + '/fixtures/data/article-1.json'),
0 ignored issues
show
Compatibility introduced by
Consider using the path module for constructing paths since they are otherwise not cross-OS compatible.
Loading history...
22
          jsonHomepage: fse.readJsonSync(__dirname + '/fixtures/data/homepage-1.json')
0 ignored issues
show
Compatibility introduced by
Consider using the path module for constructing paths since they are otherwise not cross-OS compatible.
Loading history...
23
        }
24
        done()
25
        
26
      }.bind(this))
27
  });
28
29
  /**
30
   * Util.getAllAttributes
31
   * 
32
   */
33
  it('Util.getAllAttributes()', function(done) {
34
    var attributes = Util.getAllAttributes(this.fixture.tag, this.fixture.jsonArticle)
35
    chai.expect(attributes.sourceString).to.contain('select');
36
    done();
37
  });
38
39
  /**
40
   * cmsData.sql.executeQuery
41
   * 
42
   */
43
  it('cmsData.sql.executeQuery()', function(done) {
44
    try {
45
      var match = 'select * from ../'
46
      var jsonPage = {}
0 ignored issues
show
Unused Code introduced by
The variable jsonPage seems to be never used. Consider removing it.
Loading history...
47
      var res = cmsData.sql.handleSqlRequest(match, {})
48
49
      chai.assert.equal(res.string, 'select ["*"] from ["___abe_dot______abe_dot______abe___"] ', 'select not well formatted')
50
      done();
51
    } catch (x) {
52
      done(x);
53
    }
54
  });
55
56
  /**
57
   * cmsData.sql.executeFromClause
58
   * 
59
   */
60
  it('cmsData.sql.executeFromClause()', function() {
61
    var res = cmsData.sql.executeFromClause(['/'], ['/'])
62
    chai.expect(res).to.have.length(2);
63
  });
64
65
  it('cmsData.sql.executeWhereClause() =', function() {
66
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template`=`article`', {})
67
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
68
    chai.expect(res, '`abe_meta.template`=`article`').to.have.length(1);
69
    chai.assert.equal(res[0].title, 'article', 'expected select to find article but found ' + res[0].title);
70
71
    request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template`=`{{abe_meta.template}}`', this.fixture.jsonHomepage)
72
    res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, this.fixture.jsonHomepage)
73
    chai.expect(res, '`abe_meta.template`=`{{abe_meta.template}}`').to.have.length(1);
74
    chai.assert.equal(res[0].title, 'homepage', 'expected select to find homepage but found ' + res[0].title);
75
76
    request = cmsData.sql.handleSqlRequest('select title from ./ where `{{abe_meta.template}}`=`{{abe_meta.template}}`', this.fixture.jsonHomepage)
77
    res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, this.fixture.jsonHomepage)
78
    chai.expect(res, '`{{abe_meta.template}}`=`{{abe_meta.template}}`').to.have.length(1);
79
    chai.assert.equal(res[0].title, 'homepage', 'expected select to find homepage but found ' + res[0].title);
80
  });
81
  it('cmsData.sql.executeWhereClause() !=', function() {
82
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template`!=`homepage`', {})
83
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
84
    chai.expect(res, '`abe_meta.template`!=`homepage`').to.have.length(1);
85
    chai.assert.equal(res[0].title, 'article', 'expected select to find article but found ' + res[0].title);
86
  });
87
  it('cmsData.sql.executeWhereClause() >', function() {
88
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `priority`>`1`', {})
89
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
90
    chai.expect(res, '`priority`>`1`').to.have.length(1);
91
    chai.assert.equal(res[0].title, 'article', 'expected select to find article but found ' + res[0].title);
92
  });
93
  it('cmsData.sql.executeWhereClause() >=', function() {
94
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `priority`>=`1`', {})
95
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
96
    chai.expect(res, '`priority`>=`1`').to.have.length(2);
97
  });
98
  it('cmsData.sql.executeWhereClause() <', function() {
99
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `priority`<`1`', {})
100
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
101
    chai.expect(res, '`priority`<`1`').to.have.length(0);
102
  });
103
  it('cmsData.sql.executeWhereClause() <=', function() {
104
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `priority`<=`1`', {})
105
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
106
    chai.expect(res, ' `priority`<=`1`').to.have.length(1);
107
    chai.assert.equal(res[0].title, 'homepage', 'expected select to find homepage but found ' + res[0].title);
108
  });
109
  it('cmsData.sql.executeWhereClause() LIKE', function() {
110
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template` LIKE `home`', {})
111
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
112
    chai.expect(res, '`abe_meta.template` LIKE `home`').to.have.length(1);
113
    chai.assert.equal(res[0].title, 'homepage', 'expected select to find homepage but found ' + res[0].title);
114
  });
115
  it('cmsData.sql.executeWhereClause() NOT LIKE', function() {
116
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template` NOT LIKE `home`', {})
117
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
118
    chai.expect(res, '`abe_meta.template` NOT LIKE `home`').to.have.length(1);
119
    chai.assert.equal(res[0].title, 'article', 'expected select to find article but found ' + res[0].title);
120
  });
121
  it('cmsData.sql.executeWhereClause() AND', function() {
122
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template`=`homepage` AND title=`homepage`', {})
123
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
124
    chai.expect(res, '`abe_meta.template`=`homepage` AND title=`homepage`').to.have.length(1);
125
    chai.assert.equal(res[0].title, 'homepage', 'expected select to find homepage but found ' + res[0].title);
126
  });
127
  it('cmsData.sql.executeWhereClause() OR', function() {
128
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template`=`homepage` OR `abe_meta.template`=`article`', {})
129
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
130
    chai.expect(res, '`abe_meta.template`=`homepage` OR `abe_meta.template`=`article`').to.have.length(2);
131
  });
132
  it('cmsData.sql.executeWhereClause() IN', function() {
133
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template` IN (`homepage`,`test`)', {})
134
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
135
    chai.expect(res, '`abe_meta.template` IN (`homepage`,`test`)').to.have.length(1);
136
    chai.assert.equal(res[0].title, 'homepage', 'expected select to find homepage but found ' + res[0].title);
137
138
    request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template` IN (`{{articles}}`)', this.fixture.jsonHomepage)
139
    res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, this.fixture.jsonHomepage)
140
    chai.expect(res, '`abe_meta.template` IN (`{{articles}}`').to.have.length(1);
141
    chai.assert.equal(res[0].title, 'article', 'expected select to find article but found ' + res[0].title);
142
  });
143
  it('cmsData.sql.executeWhereClause() NOT IN', function() {
144
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template` NOT IN (`homepage`,`test`)', {})
145
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
146
    chai.expect(res, '`abe_meta.template` NOT IN (`homepage`,`test`)').to.have.length(1);
147
    chai.assert.equal(res[0].title, 'article', 'expected select to find article but found ' + res[0].title);
148
149
    request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template` NOT IN (`{{articles}}`)', this.fixture.jsonHomepage)
150
    res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, this.fixture.jsonHomepage)
151
    chai.expect(res, '`abe_meta.template` NOT IN (`{{articles}}`)').to.have.length(1);
152
    chai.assert.equal(res[0].title, 'homepage', 'expected select to find homepage but found ' + res[0].title);
153
  });
154
155
  /**
156
   * cmsData.sql.whereLike
157
   * 
158
   */
159
  it('cmsData.sql.getSourceType()', function() {
160
    chai.expect(cmsData.sql.getSourceType('http://google.com')).to.equal('url');
161
    chai.expect(cmsData.sql.getSourceType('select * from test')).to.equal('request');
162
    chai.expect(cmsData.sql.getSourceType('{"test":"test"}')).to.equal('value');
163
    chai.expect(cmsData.sql.getSourceType('references.json')).to.equal('file');
164
    chai.expect(cmsData.sql.getSourceType('test')).to.equal('other');
165
  });
166
167
  /**
168
   * cmsData.source.requestList
169
   * 
170
   */
171
  it('cmsData.source.requestList()', function(done) {
172
    var matches = cmsData.regex.getTagAbeTypeRequest(this.fixture.tag)
173
174
    chai.expect(matches[0][0]).to.not.be.null
0 ignored issues
show
introduced by
The result of the property access to chai.expect(matches.0.0).to.not.be.null is not used.
Loading history...
175
176
    var attributes = Util.getAllAttributes(matches[0][0], {})
177
    chai.expect(matches[0][0]).to.not.be.null
0 ignored issues
show
introduced by
The result of the property access to chai.expect(matches.0.0).to.not.be.null is not used.
Loading history...
178
179
    var jsonPage = {}
180
    cmsData.source.requestList(attributes, '', matches[0][0], jsonPage)
181
      .then(function () {
182
        chai.expect(jsonPage.abe_source).to.not.be.undefined
0 ignored issues
show
introduced by
The result of the property access to chai.expect(jsonPage.abe...ce).to.not.be.undefined is not used.
Loading history...
183
        done()
184
      })
185
  });
186
});
187